package com.example.demo.service.impl;

import com.example.demo.mapper.JilueMapper;
import com.example.demo.pojo.Jilue;
import com.example.demo.service.JiLueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class JiLueServiceImlpl implements JiLueService {
@Autowired
    private JilueMapper jilueMapper;

    // 查询所有记录或根据查询条件模糊查询
    @Override
    public List<Jilue> getAllJilues(String query) {
        return jilueMapper.findAll(query);
    }

    // 添加新记录
    @Override
    public Jilue addJilue(Jilue jilue) {
        int rows = jilueMapper.insertJilue(jilue);
        if (rows > 0) {
            return jilue;
        }
        throw new RuntimeException("添加记录失败");
    }

    // 更新记录
    @Override
    public Jilue updateJilue(Long id, Jilue jilue) {
        // 设置 ID
        jilue.setId(id);
        int rows = jilueMapper.updateJilue(jilue);
        if (rows > 0) {
            return jilue;
        }
        throw new RuntimeException("更新记录失败");
    }

    // 删除记录
    @Override
    public void deleteJilue(Long id) {
        int rows = jilueMapper.deleteJilue(id);
        if (rows == 0) {
            throw new RuntimeException("删除记录失败");
        }
    }

    @Override
    public boolean existsById(Long id) {
        return jilueMapper.findById(id) != null; // 如果找到记录，则返回 true
    }
}
